#!/bin/bash
arg_name=""
true="true"
false="false"
run_count=1
date=$(date +'%Y%m%d%H%M%S')
swift_path=ts-swift-workload
swift_sdk="../../../tools/swift-5.8.1-RELEASE-ubuntu20.04/usr/bin/"
android_ndk="../../../tools/android-ndk-r25c/"
toolspath=./toolspath.txt
function help_explain(){
    echo "[--help,--h]"
    echo "[--date <date>]"
    echo "[--run-count <count>]"
    echo "[--case <case-name>]"
    echo "[--build]"
    echo "[--excel]"
    echo "[--aarch64]"
    exit
}

if [ -s "$toolspath" ]; then
    while IFS= read -r line; do
        case $line in
            *--swift-tools-path\ *)
                swift_sdk="${line#*--swift-tools-path\ }"
                ;;
            *--case-path\ *)
                ts_path="${line#*--case-path\ }"
                ;;
            *--android-ndk\ *)
                android_ndk="${line#*--android-ndk\ }"
                ;;
            *--Ninja-ReleaseAssert\ *)
                Ninja_ReleaseAssert="${line#*--Ninja-ReleaseAssert\ }"
                ;;
        esac
    done < "toolspath.txt"
fi

if [ -z "$1" ]; then
    echo "No input provided."
else
    for arg in "$@"; do
        case $arg_name in
            "--case")
                arg_name=""
                case_name=$arg
                ;;
            "--date")
                arg_name=""
                date=$arg
                ;;
            "--run-count")
                arg_name=""
                run_count=$arg
                ;;
        esac
        case $arg in
            "--h")
                help_explain
                ;;
            "--help")
                help_explain
                ;;
            "--build")
                build="true"
                ;;
            "--case")
                arg_name=$arg
                ;;
            "--date")
                arg_name=$arg
                ;;
            "--excel")
                excel="true"
                ;;
            "--run-count")
                arg_name=$arg
                ;;
            "--aarch64")
                aarch64="true"
                ;;
        esac
    done
fi

swift2path="swift_build/${date}"
tools_swiftc=${swift_sdk}/swiftc
tools_swift=${swift_sdk}/swift
function build_swift(){
    local swift_file=$1
    local file_name=$2
    echo "build : ${swift_file} to ${swift2path}"
    if [ "${aarch64}" = "true" ];then
        $tools_swiftc -O -whole-module-optimization \
        -tools-directory ${android_ndk}/toolchans/llvm/prebuilt/linux-x86_64/bin/ \
        -target aarch64-unknown-linux-android21 \
        -sdk ${android_ndk}/toolchans/llvm/prebuilt/linux-x86_64/sysroot \
        -resource-dir ${Ninja_ReleaseAssert}/swift-linux-x84_64/lib/swift \
        ${swift_file} \
        -o ${swift2path}/${file_name}
    else
        swiftc -O -whole-module-optimization ${swift_file} -o ${swift2path}/${file_name}
    fi
}

function run_swift(){
    local file_name=$1
    local swift_file=$swift2path/${file_name}
    echo ${file_name}
    $swift_file
}

function run(){
    if [[ "$build" = "true" ]]; then
        if [ ! -d $swift2path ];then
            mkdir -p $swift2path
        fi
        if [ -z "$case_name" ]; then
            for file in `ls $swift_path`
            do
                if [ -d $swift_path"/"$file ]
                then
                    if [ -d $swift_path/$file/swift ]
                    then
                        for swift_file in `ls $swift_path/$file/swift`
                        do
                            if [ ${swift_file: -6} = ".swift" ]; then
                                local swift_file_path=$swift_path/$file/swift/$swift_file
                                local file_name=$(basename "$swift_file" | cut -f1 -d'.')
                                build_swift ${swift_file_path} ${file_name}
                            fi
                        done
                    fi
                fi
            done
        else
            local swift_file_path=$(find ${swift_path} -name "${case_name}.swift")
            local file_name=$(basename "$swift_file_path" | cut -f1 -d'.')
            if [ -f "${swift_file_path}" ]; then
                build_swift ${swift_file_path} ${file_name}
            fi
        fi
    fi

    if [ ! -d $swift2path ];then
        echo "${swift2path} No such file or directory"
        exit
    fi

    if [[ "$excel" = "true" ]]; then
        if [ -z "$case_name" ]; then
            for file in `ls ${swift2path}`
            do
                if [ -f $swift2path"/"$file ];
                then
                    python swift2excel.py --dir $swift2path --run-case $file --run-count $run_count
                fi
            done
        else
            if [ -f $swift2path"/"$case_name ]; then
                python swift2excel.py --dir $swift2path --run-case $case_name --run-count $run_count
            fi
        fi
    fi
}
run